package core

import (
	"hblog/proj/app"

	"database/sql"
	"hotime"

	"fmt"
	"hblog/proj/app/mdl"
	"runtime/debug"
	"time"

	_ "github.com/mattn/go-sqlite3"

)

type config struct {
	data map[string]interface{}
}

func Init() {

	//配置实例
	c := config{hotime.Config}
	hotime.Config = c.init() //初始化配置
	DB, _ := sql.Open("sqlite3", "data.db")
	if DB.Ping() != nil {
		fmt.Println("数据库找不到")
	}
	hotime.SetDb(DB)
	hotime.Db.DB.Ping()
	go start()

	initRouter()
	hotime.Init()
}

func start() {
	unxMap := hotime.Db.Get("ball", "*", hotime.Map{"ORDER": "time DESC", "LIMIT": 1})
	t := time.Now().Unix()
	tt := t
	tt -= int64(60 * 60 * 24 * 365)
	if unxMap != nil && unxMap.GetInt64("time") != int64(0) {
		t = unxMap.GetInt64("time")
	}
	rich := mdl.Rich{}
	for true {
		t -= int64(60 * 60 * 24)
		tm := time.Unix(t, 0)
		day := tm.Format("2006-01-02")

		data := mdl.GetBall(t, day, false)

		if data != nil && len(data) != 0 {
			mdl.GetYazhi(data[0].GetString("fid"))
		}

		data = rich.Lists(t, day, false)
		if data != nil && len(data) != 0 {
			rich.Info(data[0].GetString("fid"))
		}
		debug.FreeOSMemory()

		if t < tt {
			break
		}
	}

}

//初始化本地配置参数
func (this *config) init() map[string]interface{} {
	this.data["port"] = "8080"
	this.data["tpt"] = "tpt"
	this.data["cached"] = false
	this.data["defFile"] = []string{"index.html", "index.htm"}
	return this.data
}
func initRouter() {

	hotime.Router(app.UserCtr{})
	hotime.Router(app.BallCtr{})
}
